package com.itheima.dao.system;

import com.itheima.domain.system.Module;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.Map;

/**
 * 平台系统管理 -> 模块管理(菜单管理)
 */
public interface ModuleDao {
    @Select("select * from ss_module")
    @Results(id = "moduleMap",value = {
        @Result(property = "id", column = "module_id",jdbcType = JdbcType.VARCHAR),
        @Result(property = "parentId", column = "parent_id",jdbcType = JdbcType.VARCHAR),
        @Result(property = "name", column = "name",jdbcType = JdbcType.VARCHAR),
        @Result(property = "ctype", column = "ctype",jdbcType = JdbcType.VARCHAR),
        @Result(property = "state", column = "state",jdbcType = JdbcType.VARCHAR),
        @Result(property = "curl", column = "curl",jdbcType = JdbcType.VARCHAR),
        @Result(property = "remark", column = "remark",jdbcType = JdbcType.VARCHAR),
        @Result(column = "parent_id",property = "module", javaType = Module.class,one = @One(select = "com.itheima.dao.system.ModuleDao.findById"))
    })
    List<Module> findAll() throws Exception;

    @Select("select * from ss_module where module_id = #{id}")
    @ResultMap("moduleMap")
    Module findById(String id) throws Exception;

    @Insert("INSERT INTO ss_module (module_id, parent_id, NAME, ctype, state, curl, remark) VALUES (#{id,jdbcType=VARCHAR}, #{parentId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{ctype,jdbcType=DECIMAL}, #{state,jdbcType=DECIMAL}, #{curl,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR})")
    int save(Module module) throws Exception;

    @Update("UPDATE ss_module SET parent_id = #{parentId,jdbcType=VARCHAR}, NAME = #{name,jdbcType=VARCHAR}, ctype = #{ctype,jdbcType=DECIMAL}, state = #{state,jdbcType=DECIMAL}, curl = #{curl,jdbcType=VARCHAR}, remark = #{remark,jdbcType=VARCHAR} WHERE module_id = #{id,jdbcType=VARCHAR}")
    int update(Module module)throws Exception;

    @Delete("delete from ss_module where module_id = #{id,jdbcType=VARCHAR}")
    int deleteById(String id)throws Exception;

    @Select("SELECT module_id 'id', parent_id 'pId', NAME 'name', CASE WHEN module_id IN (SELECT module_id FROM ss_role_module WHERE role_id = #{xx}) THEN 'true' ELSE 'false' END 'checked' FROM ss_module")
    List<Map>findTreeData(String roleId)throws Exception;
}
